package com.app.micaihu.custom.view.scrollview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;
import android.widget.TextView;

import com.app.utils.util.ViewSizeUtils;

public class ImageScrollView extends ScrollView {
    private int actionDownY;
    private boolean allowClick;
    private int viewHight;
    private int scrollHight;
    private int scrollY1;
    private TextView textView;

    public void setView(TextView textView) {
        this.textView = textView;
        getMHight();
    }

    private void getMHight() {

        viewHight = ViewSizeUtils.getHeightPixels() - ViewSizeUtils.dp2px(getContext(), 180);
        scrollHight = ViewSizeUtils.getHeightPixels() - ViewSizeUtils.dp2px(getContext(), 400);
    }

    public boolean isAllowClick() {
        return allowClick;
    }

    public void setAllowClick(boolean allowClick) {
        this.allowClick = allowClick;
    }

    public ImageScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public ImageScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public ImageScrollView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        // TODO Auto-generated method stub
        super.onScrollChanged(l, t, oldl, oldt);
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX,
                                   int scrollY, int scrollRangeX, int scrollRangeY,
                                   int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
        // TODO Auto-generated method stub
        if (actionDownY < viewHight) {

        } else if (scrollY1 != scrollY) {
            viewHight -= (scrollY - scrollY1);
            scrollHight += (scrollY - scrollY1);
            scrollY1 = scrollY;
        }
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
                scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        try {
            // TODO Auto-generated method stub
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (textView != null) {
                        if (scrollHight == 0) {
                            getMHight();
                        }
                        actionDownY = (int) ev.getY() + scrollHight;

                        if (textView.getLineCount() < 3 || actionDownY < viewHight) {
                            setFocusable(false);
                            getParent().requestDisallowInterceptTouchEvent(false);
                            return false;
                        } else if (textView.getLineCount() >= 3) {
                            setFocusable(true);
                            getParent().requestDisallowInterceptTouchEvent(true);
                        } else {
                            setFocusable(false);
                            getParent().requestDisallowInterceptTouchEvent(false);
                            return false;
                        }
                    }
                    break;
                case MotionEvent.ACTION_MOVE:

                    break;
                default:

                    break;
            }
            return super.dispatchTouchEvent(ev);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

}
